//
//  SpotInformationCell.swift
//  swiftProject
//
//  Created by ZhangSai on 2021/7/2.
//

import UIKit

protocol SpotInformationCellDelegate:class {
    func spotprintInfoCell(cell:SpotInformationCell,clickType:SpotInfoCellType)
}

class SpotInformationCell: UITableViewCell {

    @IBOutlet weak var infoLab: UILabel!
    @IBOutlet weak var infoHintLab: UILabel!
    @IBOutlet weak var timerLab: UILabel!
    
    @IBOutlet weak var infoMsgLab: UILabel!
    @IBOutlet weak var payMsgLab: UILabel!
    @IBOutlet weak var timerMsgLab: UILabel!
    
    @IBOutlet weak var payLab: UILabel!

    @IBOutlet weak var infoView: UIView!
    @IBOutlet weak var payView: UIView!
    @IBOutlet weak var timerView: UIView!
    @IBOutlet weak var bgView: UIView!

    @IBOutlet weak var deliverView: UIView!
    @IBOutlet weak var deliverLab: UILabel!
    @IBOutlet weak var deliverDetailLab: UILabel!

    weak var delegate:SpotInformationCellDelegate?
    var orderInfoModel:OrderListModel!

    override func awakeFromNib() {
        super.awakeFromNib()
        let tapGR = UITapGestureRecognizer.init(target: self, action: #selector(infoAction))
        infoView.isUserInteractionEnabled = true
        infoView.addGestureRecognizer(tapGR)
        
        let addressTap = UITapGestureRecognizer.init(target: self, action: #selector(payAction))
        payView.isUserInteractionEnabled = true
        payView.addGestureRecognizer(addressTap)
        
        let lookExpressTap = UITapGestureRecognizer.init(target: self, action: #selector(timerAction))
        timerView.isUserInteractionEnabled = true
        timerView.addGestureRecognizer(lookExpressTap)
        
        let deliversTap = UITapGestureRecognizer.init(target: self, action: #selector(deliversAction))
        deliverView.isUserInteractionEnabled = true
        deliverView.addGestureRecognizer(deliversTap)
        
        self.contentView.backgroundColor = UIColor.init(hexString: "#F3F3F3")
        bgView.layer.cornerRadius = 5
        bgView.layer.shadowColor = UIColor.init(red: 0, green: 0, blue: 0, alpha: 0.2).cgColor
        bgView.layer.shadowOffset = CGSize.init(width: 0, height: 1)
        bgView.layer.shadowOpacity = 1
        bgView.layer.shadowRadius = 2;
        bgView.layer.cornerRadius = 5;
        infoView.layer.cornerRadius = 5;
        timerView.layer.cornerRadius = 5;
        
        infoHintLab.layer.borderColor = UIColor.init(hexString: "#F48080").cgColor;
        infoHintLab.layer.borderWidth = 0.5;
        infoHintLab.layer.cornerRadius = 7;
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    func setOrderListModel(orderInfoModel:OrderListModel) {
        self.orderInfoModel = orderInfoModel
        infoLab.text = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_goodsMsg)
        infoHintLab.text = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_required)
        payLab.text = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_paymentMethod)
        deliverLab.text = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_shippingMethod)
        
        if orderInfoModel.goodsName.count <= 0 {
            infoMsgLab.text = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_pleaseSelect)
            infoMsgLab.textColor = UIColor.init(hexString: "#B2B2B2")
        } else {
            infoMsgLab.textColor = UIColor.init(hexString: "#333333")
            let volume =  orderInfoModel.goodsLength * orderInfoModel.goodsWidth *  orderInfoModel.goodsHeight/1728
            orderInfoModel.goodsSize = String.init(format: "%.2f", volume)
            if volume > 0 {
                if volume > 16 {
                    infoMsgLab.text = orderInfoModel.goodsName + String(orderInfoModel.goodsWidth) + "16ft³"
                } else {
                    infoMsgLab.text = orderInfoModel.goodsName + String(orderInfoModel.goodsWidth) + String.init(format: "%.2f", volume) + "ft³"
                }
            } else {
                infoMsgLab.text = orderInfoModel.goodsName + String(orderInfoModel.goodsWidth) + "kg"
            }
        }
        
        let roleType = WLUserModel.getRoleType()
        if orderInfoModel.payType == 0 {
            payMsgLab.text = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_paidShipper)
            if roleType.contains(where: { (obj) -> Bool in
                if obj as! Int == 7 {
                    return true
                } else {
                    payMsgLab.textColor = UIColor.init(hexString: "#333333")
                    return false
                }
            }) {
                
            }
        } else if (orderInfoModel.payType == 1) {
            payMsgLab.text = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_payType_topay)
        } else if(orderInfoModel.payType == 2){
            payMsgLab.text = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_payType_deduction)
        } else if (orderInfoModel.payType == 3) {
            payMsgLab.text = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.order_bill_calculate)
        }
        
        if orderInfoModel.deliveryWay == 0 {
            deliverDetailLab.text =  ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_deliver_visit)
            deliverDetailLab.textColor = UIColor.init(hexString: "#333333")
        } else if (orderInfoModel.deliveryWay == 1) {
            deliverDetailLab.text =  ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_deliver_stationt)
        } else if (orderInfoModel.deliveryWay == 2) {
            deliverDetailLab.text =  ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_deliver_airport)
        }
        
        timerLab.text = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_expectDeliveryTimer)
        if orderInfoModel.orderType == 2 {
            if orderInfoModel.expectedDeliveryTime.count > 0 {
                timerMsgLab.textColor = UIColor.init(hexString: "#333333")
                timerMsgLab.text = orderInfoModel.expectedDeliveryTime + orderInfoModel.expectedDeliveryTimePeriod
            } else {
                timerMsgLab.textColor = UIColor.init(hexString: "#333333")
                timerMsgLab.text = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_timer_standard)
            }
        } else if (orderInfoModel.orderType == 0) {
            if orderInfoModel.standardTimerValue.count > 0 {
                timerMsgLab.textColor = UIColor.init(hexString: "#333333")
                timerMsgLab.text = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_timer_standard)
            } else {
                timerMsgLab.textColor = UIColor.init(hexString: "#333333")
                timerMsgLab.text = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_timer_standard)
            }
        } else {
            timerMsgLab.textColor = UIColor.init(hexString: "#333333")
            timerMsgLab.text = ZslanguageTool.sharedLanguageTool().getIanguageWithKey(key: LanguageHeader.WL_spot_timer_urgent)
        }
    }
    
    @objc func infoAction() {
        delegate?.spotprintInfoCell(cell: self, clickType: SpotInfoCellType.Spot_InfoMsg_Type)
    }
    @objc func payAction() {
        delegate?.spotprintInfoCell(cell: self, clickType: SpotInfoCellType.Spot_PayMsg_Type)
    }
    @objc func timerAction() {
        delegate?.spotprintInfoCell(cell: self, clickType: SpotInfoCellType.Spot_TimerMsg_Type)
    }
    @objc func deliversAction() {
        delegate?.spotprintInfoCell(cell: self, clickType: SpotInfoCellType.Spot_Delivery_Type)
    }
}
